function getMonthDays(month) {
    var mdays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    return mdays[month - 1];
}

function festival(day, term) {
    var f = [];
    f["1-1"] = '元旦';
    f["4-5"] = '清明';
    f["5-1"] = '五一';
    f["10-1"] = '国庆';

    return f[day] != undefined ? f[day] : term;
}

function isXiuBan(day){
    var f = [];
    f["1-1"] = 2;
    f["1-26"] = 1;
    f["1-28"] = 2;
    f["1-29"] = 2;
    f["1-30"] = 2;
    f["1-31"] = 2;
    f["2-1"] = 2;
    f["2-2"] = 2;
    f["2-3"] = 2;
    f["2-4"] = 2;
    f["2-8"] = 1;
    f["4-4"] = 2;
    f["4-5"] = 2;
    f["4-6"] = 2;
    f["4-27"] = 1;
    f["5-1"] = 2;
    f["5-2"] = 2;
    f["5-3"] = 2;
    f["5-4"] = 2;
    f["5-5"] = 2;
    f["5-31"] = 2;
    f["6-1"] = 2;
    f["6-2"] = 2;
    f["6-3"] = 2;
    f["9-28"] = 1;
    f["10-1"] = 2;
    f["10-2"] = 2;
    f["10-3"] = 2;
    f["10-4"] = 2;
    f["10-5"] = 2;
    f["10-6"] = 2;
    f["10-7"] = 2;
    f["10-8"] = 2;
    f["10-11"] = 1;

    return f[day] != undefined ? f[day] : '';
}

function createCalendar(y, m) {
    var days = 0;
    if (m == 2) {
        var isLeap = isLeapYear(y);
        days = getMonthDays(m);
        if (isLeap) {
            days++;
        }
    } else {
        days = getMonthDays(m);
    }
    var gzYear = calendar.solar2lunar(y, m, 1).gzYear;
    gzYear += calendar.solar2lunar(y, m, 1).Animal + "年";
    var data = {
        year: y,
        month: m,
        nyear: gzYear,
        days: []
    };
    var now = new Date().Format("yyyy-MM-dd");
    for (i = 0; i < days; i++) {
        var term = calendar.solar2lunar(y, m, i + 1).Term;
        var ndayAndTerm = term != null ? term : calendar.solar2lunar(y, m, i + 1).IDayCn;
        //ndayAndTerm = [m, i + 1].join('-') == "1-1" ? "元旦" : ndayAndTerm;
        ndayAndTerm = festival([m, i + 1].join('-'), ndayAndTerm);
        var xiuban = isXiuBan([m, i + 1].join('-')) != '' ? isXiuBan([m, i + 1].join('-')) == 1 ? '班'  : '休' : '';
        var banNumber = getBanName(y, m, i + 1);
        var ds = {
            day: i + 1,
            style: "#000000",
            tag: [y, m, i + 1].join('/'),
            week: calendar.solar2lunar(y, m, i + 1).nWeek,
            nmonth: calendar.solar2lunar(y, m, i + 1).IMonthCn,
            border: 0,
            nday: ndayAndTerm,
            xiuban : xiuban,
            ban: getBan(banNumber),
            banStyle: getBanStyle(banNumber)

        }
        var nm = m;
        var nd = i + 1;
        if (nm < 10) {
            nm = "0" + nm;
        }
        if (nd < 10) {
            nd = "0" + nd;
        }
        if (now == [y, nm, nd].join('-')) {
            ds.border = 1;
        }
        if (ds.week == 6 || ds.week == 7 || [m, i + 1].join('-') == "1-1") {
            ds.style = "red";
        }
        data.days.push(ds);
    }
    //上个月的 
    if (data.days[0].week < 7) {
        console.log(data.days[0].week);
        var c = data.days[0].week;
        var sy = y;
        var sm = m;
        if (sm == 1) {
            sy = y - 1;
            sm = 13;
        }
        console.log(sm, getMonthDays(sm - 1));
        for (i = getMonthDays(sm - 1); i > getMonthDays(sm - 1) - c; i--) {
            var banNumber = getBanName(sy, sm - 1, i);
            var xiuban = isXiuBan([sm, i + 1].join('-')) != '' ? isXiuBan([sm, i + 1].join('-')) == 1 ? '班'  : '休' : '';
            var ds = {
                day: i,
                style: "#CCCCCC",
                tag: [sy, sm - 1, i].join('/'),
                week: calendar.solar2lunar(sy, sm - 1, i).nWeek,
                nmonth: calendar.solar2lunar(sy, sm - 1, i).IMonthCn,
                nday: calendar.solar2lunar(sy, sm - 1, i).IDayCn,
                xiuban:xiuban,
                ban: getBan(banNumber),
                banStyle: getBanStyle(banNumber),
                ck: [sy, sm - 1]
            }
            data.days.unshift(ds);
        }
    }
    //下个月的
    if (data.days[data.days.length - 1].week <= 7) {
        var w = data.days[data.days.length - 1].week;
        w = w == 7 ? 0 : w;
        var xy = y;
        var xm = m;
        if (data.month == 12) {
            xy = y + 1;
            xm = 1;
        } else {
            xm = m + 1;
        }
        for (i = 0; i < 6 - w; i++) {
            var ndayAndTerm = term != null ? term : calendar.solar2lunar(xy, xm, i + 1).IDayCn;
            //ndayAndTerm = [xm, i + 1].join('-') == "1-1" ? "元旦" : ndayAndTerm;
            ndayAndTerm = festival([xm, i + 1].join('-'), ndayAndTerm);
            var xiuban = isXiuBan([xm, i + 1].join('-')) != '' ? isXiuBan([xm, i + 1].join('-')) == 1 ? '班'  : '休' : '';
            var banNumber = getBanName(xy, xm, i + 1);
            var ds = {
                day: i + 1,
                style: "#CCCCCC",
                tag: [xy, xm, i + 1].join('/'),
                week: calendar.solar2lunar(xy, xm, i + 1).nWeek,
                nmonth: calendar.solar2lunar(xy, xm, i + 1).IMonthCn,
                nday: ndayAndTerm,
                xiuban:xiuban,
                ban: getBan(banNumber),
                banStyle: getBanStyle(banNumber),
                ck: [xy, xm]
            }
            data.days.push(ds);
        }
    }
    return data;
}

var flag = 8;

function getBan(num) {
    //0 休　１　白(主) 3 白(副) 5夜　8下
    var ban = ['休', '白', '', '白', '', '夜', '', '', '下'];
    return ban[num];
}

function getBanStyle(num) {
    //0 休　１　白(主) 3 白(副) 5夜　8下
    var ban = ['label label-danger', 'label label-primary', '', 'label label-info', '', 'label label-default', '', '', 'label label-danger'];
    return ban[num];
}

function getBan_old() {
    //0 休　１　白(主) 3 白(副) 5夜　8下
    var ban = ['', '下', '', '夜', '', '白', '', '', '白'];
    var banFlag = ban[flag];
    flag >>= 1;
    if (flag == 1) {
        flag = window.localStorage['paiban'];
    }
    return banFlag;
}

function getBanName(y, m, d) {
    var b = ['5', '8', '1', '3'];
    var ban = 3;
    if (m < 10) {
        m = "0" + m;
    }
    if (d < 10) {
        d = "0" + d;
    }
    var newb = undefined;
    var num = get1900YtoNowDay([y, m, d].join('-'));
    if (window.localStorage['change'] != undefined) {
        var changeDate = window.localStorage['change'];
        var changeDateNum = get1900YtoNowDay(changeDate);
        if (changeDateNum <= num) {
            console.log(changeDateNum, num);
            if (window.localStorage['paiban'] != undefined) {
                newb = window.localStorage['paiban'].split(',');
            }
        }
    }

    //console.log(num);
    return newb == undefined ? b[num % b.length] : newb[num % newb.length];
}
